#include "Seralize.hpp"


namespace SC_SUN
{

    std::string SatelliteToJson(EvSatelliteData& sat)
    {
        using namespace rapidjson;

        Document doc = sat.ObjToJsonObject();
        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        doc.Accept(writer);
        std::string jsonStr = buffer.GetString();

        return jsonStr;
    }


    EvSatelliteData JsonToSatellite(const std::string& jsonStr)
    {
        using namespace rapidjson;

        EvSatelliteData ret;

        Document doc;
        doc.Parse(jsonStr.data());

        ret = EvSatelliteData::JsonToObj(doc);

        return ret;
    }


    std::string SatelliteListToJson(std::vector<EvSatelliteData>& sats)
    {
        using namespace rapidjson;

        Document doc;
        doc.SetArray();

        for (int i = 0; i < sats.size(); i++)
        {
            doc.PushBack(sats[i].ObjToJsonObject().GetObj(), doc.GetAllocator());
        }
        
        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        doc.Accept(writer);
        std::string jsonStr = buffer.GetString();

        return jsonStr;
    }


    std::vector<EvSatelliteData> JsonToSatelliteList(const std::string& jsonStr)
    {
        using namespace rapidjson;
        std::vector<EvSatelliteData> ret;

        Document doc;
        doc.Parse(jsonStr.data());
        auto array = doc.GetArray();

        for (int i = 0; i < array.Size(); i++)
        {
            ret.push_back(EvSatelliteData::JsonToObj2(array[i].GetObj()));
        }

        return ret;
    }


    std::string GnssToJson(EvGnssSNData& gnss)
    {
        using namespace rapidjson;

        Document doc;
        doc.SetObject();
        gnss.ObjToJsonObject(doc);

        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        doc.Accept(writer);
        std::string jsonStr = buffer.GetString();

        return jsonStr;
    }


    EvGnssSNData JsonToGnss(const std::string& jsonStr)
    {
        using namespace rapidjson;

        EvGnssSNData ret;
        Document doc;
        doc.Parse(jsonStr.data());
        ret = EvGnssSNData::JsonToObj(doc);
        return ret;
    }


    std::string SecISNToJson(EvSecIslSNData& sec)
    {
        using namespace rapidjson;

        Document doc;
        doc.SetObject();
        sec.ObjToJsonObject(doc);

        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        doc.Accept(writer);
        std::string jsonStr = buffer.GetString();

        return jsonStr;
    }


    EvSecIslSNData JsonToSecISN(const std::string& jsonStr)
    {
        using namespace rapidjson;

        EvSecIslSNData ret;
        Document doc;
        doc.Parse(jsonStr.data());
        ret = EvSecIslSNData::JsonObjectToObject2(doc);
        return ret;
    }


    std::string SceneToJson(EvSceneData& scene)
    {
        using namespace rapidjson;
        Document doc = scene.ObjToJsonObject();

        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        doc.Accept(writer);
        std::string jsonStr = buffer.GetString();

        return jsonStr;
    }


    EvSceneData JsonToScene(const std::string& jsonStr)
    {
        using namespace rapidjson;
        Document doc;
        doc.Parse(jsonStr.data());
        EvSceneData ret;
        ret = EvSceneData::JsonObjectToObj(doc);

        return ret;
    }

    KeTi2Mgr::Keti2Info Keti2JsonToObj(const std::string& jsonStr)
    {
        using namespace rapidjson;

        KeTi2Mgr::Keti2Info ret;

        Document doc;
        doc.Parse(jsonStr.data());
        auto rootObj = doc.GetObj();

        auto info = rootObj["info"].GetObj();
        ret.info.SAT_SN = info["SAT_SN"].GetString();
        ret.info.alpha = info["alpha"].GetDouble();
        ret.info.beta = info["beta"].GetDouble();

        ret.keti2C04 = rootObj["keti2C04"].GetString();
        ret.reforbit = rootObj["reforbit"].GetString();

        return ret;
    }

    std::string SceneTimeToJson(SceneTimeInfoMsg& time)
    {
        using namespace rapidjson;

        std::string ret;

        Document doc;
        doc.SetObject();

        doc.AddMember(Value("mSatStep", doc.GetAllocator()), Value(time.mSatStep), doc.GetAllocator());
        doc.AddMember(Value("mGNSSTotal", doc.GetAllocator()), Value(time.mGNSSTotal), doc.GetAllocator());
        doc.AddMember(Value("mGNSSStep", doc.GetAllocator()), Value(time.mGNSSStep), doc.GetAllocator());
        doc.AddMember(Value("mSecTotal", doc.GetAllocator()), Value(time.mSecTotal), doc.GetAllocator());
        doc.AddMember(Value("mSecStep", doc.GetAllocator()), Value(time.mSecStep), doc.GetAllocator());

        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        doc.Accept(writer);
        ret = buffer.GetString();

        return ret;
    }

    SceneTimeInfoMsg JsonToSceneTime(const std::string& jsonStr)
    {
        using namespace rapidjson;

        SceneTimeInfoMsg ret;

        Document doc;
        doc.Parse(jsonStr.data());
        auto rootObj = doc.GetObj();
        ret.mSatStep = rootObj["mSatStep"].GetDouble();
        ret.mGNSSTotal = rootObj["mGNSSTotal"].GetDouble();
        ret.mGNSSStep = rootObj["mGNSSStep"].GetDouble();
        ret.mSecTotal = rootObj["mSecTotal"].GetDouble();
        ret.mSecStep = rootObj["mSecStep"].GetDouble();

        return ret;
    }


}